from fastapi import Request, FastAPI
from fastapi.responses import JSONResponse
from app.schemas.response import ResponseModel
from app.core.exception import AppException


def register_exception(app: FastAPI):
    @app.exception_handler(AppException)
    async def app_exception_handler(request: Request, exc: AppException):
        return JSONResponse(
            status_code=200,
            content=ResponseModel(code=exc.code, msg=exc.msg).model_dump_json()
        )

    @app.exception_handler(Exception)
    async def general_exception_handler(request: Request, exc: Exception):
        # 生产环境可以隐藏详细错误信息
        return JSONResponse(
            status_code=500,
            content=ResponseModel(code=500, msg="Internal Server Error").model_dump_json()
        )
